import scrapy
from Hue.basepro import ZhengFuBaseSpider


class AnkangSpider(ZhengFuBaseSpider):
    name = 'Ankang-pro'
    allowed_domains = ['ankang.gov.cn']
    start_urls = ['http://http://www.ankang.gov.cn//']
    api = 'https://so.ankang.gov.cn/s?t=0&s=0&sid=0&n=2556,2566,2747,2869,2578,3299&q={keyword}&i=7&ctype=7&ft=0&date=&day=&p={page}'
    keywords = ['煤炭']
    method = 'GET'

    def edit_page(self, response):
        page_box = response.css('div.pager > a')
        page = page_box[-2].xpath('./text()').get()
        return int(page)

    def edit_items_box(self, response):
        items_box = response.xpath('//ul[@class="searchList"]/li')
        return items_box

    def edit_item(self, item):
        meta_info = {
            "title": item.xpath('.//h2/a/@title').get(),
            "url": item.xpath('.//h2/a/@href').get(),
            "pre_content": "".join(item.css('p *::text').getall()).strip(),
            "date": item.xpath('.//em[3]/text()').get().replace("时间：", "")
        }
        return meta_info
